<?php 
namespace xfyzf\admin\controller;
use lib\XFCaptcha;
class Ajax{//post
    private $Captcha;
    public function __construct() {
        $this->Captcha = new XFCaptcha(CaptchaCode);
    }
    public function login(){
        global $islogin_admin,$conf,$password_hash;
         if($islogin_admin==1)exitjson(-1,"您已登录");
        $postdata = input("post");
        $admin_user = $postdata['admin_user'];
        $admin_pass = $postdata['admin_pass'];
        if(!$this->Captcha->XYcaptcha($postdata['code_input']))exitjson(-1,'验证码为空或错误！');
        if(empty($admin_user) || empty($admin_pass))exitjson(-1,'账号和密码不可为空');
        if($admin_user!=$conf['admin_user']){
            createLog('0','尝试登录后台但账号错误-提交的账户为：'.$admin_user,http_ip());
            exitjson(-1,'登录失败,账号或者密码错误');
        }
        $pwd = md5(md5($admin_pass));
        if($conf['admin_pass']!=$pwd){
            createLog('0','尝试登录后台但密码错误-提交的密码为：'.$admin_pass,http_ip());
            exitjson(-1,'登录失败,账号或者密码错误');
        }
        $session=md5($admin_user.$pwd.$password_hash);
        $token=authcode("{$admin_user}\t{$session}", 'ENCODE', $conf['KEY']);
        setcookie("admin_token", $token, time() + 604800);
        createLog('0','后台登陆成功！',http_ip());
        exitjson(1,'登录成功!');
    }
    public function emailcs(){
        global $islogin_admin,$conf,$httphost;
        if($islogin_admin!=1)exitjson(-1,"请先登录！");
        $mail_name = $conf['mail_recv'] ? $conf['mail_recv'] : $conf['mail_name'];
        if(!empty($mail_name)){
             $result = send_mail($mail_name, '邮件发送测试。', '这是一封测试邮件！<br/><br/>来自：' . $httphost);
             if($result==1){
                 exitjson(200,"邮件发送成功");
             }else{
                 exitjson(-1,"邮件发送失败");
             }
         }else{
             exitjson(500,"您还未设置邮箱");
         }
    }
    public function phonecs(){
        global $islogin_admin,$conf;
        if($islogin_admin!=1)exitjson(-1,"请先登录！");
        $code = rand(111111,999999);
        exit(send_sms($conf['sms_csphone'],$code));
    }
    public function Packagestatus(){
        global $islogin_admin;
        if($islogin_admin!=1)exitjson(-1,"请先登录！");
        $id = input("post")['id'];
        if(empty($id))exitjson(-1,'非法操作！');
        $sql = "SELECT * FROM `pay_package` WHERE `id`='{$id}' limit 1";
        $row = executeSQLQuery($sql);
        if(!$row)exitjson(-1,'该套餐id不存在！');
        if($row['status'] == 1){
            $sql = "update `pay_package` set `status`='0' where id='$id' limit 1";
        }else{
            $sql = "update `pay_package` set `status`='1' where id='$id' limit 1";
        }
        if(executeSQLQuery($sql, 'update'))exitjson(200,'更新状态成功');
        exitjson(500,'更新状态失败');
    }
    public function Packageremove(){
         global $islogin_admin;
        if($islogin_admin!=1)exitjson(-1,"请先登录！");
         $id = input("post")['id'];
         $sql = "SELECT * FROM `pay_package` WHERE `id`='{$id}' limit 1";
         if(!executeSQLQuery($sql))exitjson(-1,'套餐数据不存在！');
         $sql = "DELETE FROM pay_package WHERE id='$id'";
         if(executeSQLQuery($sql, 'delete'))exitjson(200,"删除套餐成功！");
         exitjson(-1,'删除失败！');
    }
    public function Wechat_Trumpetstatus(){
        global $islogin_admin;
        if($islogin_admin!=1)exitjson(-1,"请先登录！");
        $id = input("post")['id'];
        $sql = "SELECT * FROM `pay_wechat_trumpet` WHERE `id`='{$id}' limit 1";
        $row = executeSQLQuery($sql);
        if(!$row)exitjson(-1,'该店员账号不存在 请核对后在试！');
        if($row['status'] == 1){
            $sql = "update `pay_wechat_trumpet` set `status`='0' where id='$id' limit 1";
        }else{
            $sql = "update `pay_wechat_trumpet` set `status`='1' where id='$id' limit 1";
        }
        if(executeSQLQuery($sql, 'update'))exitjson(200,'更新状态成功');
        exitjson(500,'更新状态失败');
    }
    public function Wechat_Trumpetremove(){
        global $islogin_admin;
        if($islogin_admin!=1)exitjson(-1,"请先登录！");
        $id = input("post")['id'];
        if(empty($id))exitjson(-1,'非法操作！');
        $sql = "SELECT * FROM `pay_wechat_trumpet` WHERE `id`='{$id}' limit 1";
        if(!executeSQLQuery($sql))exitjson(-1,'店员数据不存在！');
        $sql = "DELETE FROM pay_wechat_trumpet WHERE id='$id'";
         if(executeSQLQuery($sql, 'delete'))exitjson(200,"删除店员数据成功！");
         exitjson(-1,'删除失败！');
    }
    public function wxpay_zt_Out(){
        global $islogin_admin,$date;
        if($islogin_admin!=1)exitjson(-1,"请先登录！");
        $postdata      = input("post");
        $wx_username   = $postdata['wx_username'];
        $wx_dyname     = $postdata['wx_dyname'];
        if(empty($wx_dyname) || empty($wx_username))exitjson(-1,'所有内容不可为空！');
        $sql = "update `pay_qrlist` set `status`='1',`endtime`='{$date}',`wx_name`='{$wx_dyname}',`type`='wxpay' WHERE `beizhu`='{$wx_username}'";
        if(executeSQLQuery($sql, 'update'))exitjson(200,'强制绑定成功！');
        exitjson(500,'强制绑定失败！');
    }
    public function Del_log(){
        global $islogin_admin;
        if($islogin_admin!=1)exitjson(-1,"请先登录！");
        $id = input("post")['id'];
        if(empty($id))exitjson(-1,'非法操作!');
        $sql = "SELECT * FROM `pay_log` WHERE `id`='{$id}' limit 1";
        if(!executeSQLQuery($sql))exitjson(-1,'数据不存在！');
        $sql="DELETE FROM `pay_log` WHERE `id`='{$id}' limit 1";
        if(executeSQLQuery($sql, 'delete')){
            createLog('admin','删除日志成功ID：'.$id,http_ip());
            exitjson(200,"删除日志成功！");
        }
        exitjson(-1,'删除失败！');
    }
    public function Del_qblog(){
        global $islogin_admin;
       if($islogin_admin!=1)exitjson(-1,"请先登录！");
       $sql ="SELECT count(*) as count from pay_log WHERE 1";
       $count = executeSQLQuery($sql)['count'];
       if(!$count){
           exitjson(-1,'一键删除日志失败 当前无日志！');
       }
       $sql ="DELETE FROM pay_log WHERE 1";
       if(executeSQLQuery($sql, 'delete')){
            createLog('admin','一键删除日志成功 共删除：'.$count.'个记录',http_ip());
           exitjson(200,'一键删除日志成功！');
       }
       exitjson(-1,'删除失败！');
    }
    public function Noticestatus(){
        global $islogin_admin;
       if($islogin_admin!=1)exitjson(-1,"请先登录！");
       $id = input("post")['id'];
       $sql = "SELECT * FROM `pay_notice` WHERE `id`='{$id}' limit 1";
       $row = executeSQLQuery($sql);
       if(!$row)exitjson(-1,'此公告数据不存在 请核对后在试！');
       if($row['status'] == 1){
            $sql = "update `pay_notice` set `status`='0' where id='$id' limit 1";
        }else{
            $sql = "update `pay_notice` set `status`='1' where id='$id' limit 1";
        }
        if(executeSQLQuery($sql, 'update'))exitjson(200,'更新状态成功');
        exitjson(500,'更新状态失败');
    }
    public function Noticeremove(){
         global $islogin_admin;
       if($islogin_admin!=1)exitjson(-1,"请先登录！");
       $id = input("post")['id'];
       $sql = "SELECT * FROM `pay_notice` WHERE `id`='{$id}' limit 1"; 
       if(!executeSQLQuery($sql))exitjson(-1,'此公告数据不存在！');
       $sql ="DELETE FROM pay_notice WHERE `id`='{$id}' limit 1";
       if(executeSQLQuery($sql, 'delete')){
            createLog('admin','删除公告成功ID：'.$id,http_ip());
            exitjson(200,"删除日志成功！");
        }
        exitjson(-1,'删除失败！');
    }
    public function downremove(){
        global $islogin_admin;
       if($islogin_admin!=1)exitjson(-1,"请先登录！");
       $id = input("post")['id'];
       $sql = "SELECT * FROM `pay_down` WHERE `id`='{$id}' limit 1"; 
       if(!executeSQLQuery($sql))exitjson(-1,'此插件数据不存在！');
       $sql ="DELETE FROM pay_down WHERE `id`='{$id}' limit 1";
       if(executeSQLQuery($sql, 'delete')){
            createLog('admin','删除插件数据成功 ID：'.$id,http_ip());
            exitjson(200,"删除插件数据成功！");
        }
        exitjson(-1,'删除失败！');
    }
    public function workremove(){
       global $islogin_admin;
       if($islogin_admin!=1)exitjson(-1,"请先登录！");
       $id = input("post")['id'];
       $sql = "SELECT * FROM `pay_work` WHERE `id`='{$id}' limit 1"; 
       if(!executeSQLQuery($sql))exitjson(-1,'此工单数据不存在！');
       $sql ="DELETE FROM pay_work WHERE `id`='{$id}' limit 1";
       if(executeSQLQuery($sql, 'delete')){
           createLog('admin','删除工单数据成功 ID：'.$id,http_ip());
           exitjson(200,"删除工单数据成功！");
       }
       exitjson(-1,'删除失败！');
    }
    public function up(){
      global $islogin_admin;
      if($islogin_admin!=1)exitjson(-1,"请先登录");
          $url = XFAPI_URL. '/api_check.php?url=' . $_SERVER['HTTP_HOST'] . '&authcode=' . xfauthcode . '&ver=' . Version . '&cxid=' . xfcxid;
          $result=get_curl($url);
          $res=json_decode($result,true);
          if($res['code']!=1)exitjson(-1,'暂时无法更新');
          $RemoteFile = $res['file'];
         
          $ZipFile = "updata.zip";
          
          download_zip($ZipFile,$RemoteFile);
          if (zipExtract($ZipFile,ROOT)){
              $up=get_zip_originalsize($ZipFile,ROOT);
            //   unlink($ZipFile);
              exitjson(1,"程序更新成功！(".$up.")");
          }else{
              if (file_exists($ZipFile)){
                //   unlink($ZipFile);
                  exitjson(-1,'无法解压文件');
              }
          }
    }
    public function emailallRemove(){
        global $islogin_admin;
       if($islogin_admin!=1)exitjson(-1,"请先登录！");
       $sql ="SELECT count(*) as count from pay_email WHERE 1";
       $count = executeSQLQuery($sql)['count'];
       if(!$count){
           exitjson(-1,'一键删除发信记录失败 当前无日志！');
       }
       $sql ="DELETE FROM pay_email WHERE 1";
       if(executeSQLQuery($sql, 'delete')){
            createLog('admin','一键删除发信记录成功 共删除：'.$count.'个记录',http_ip());
           exitjson(200,'一键删除发信记录成功！');
       }
       exitjson(-1,'删除失败！');
    }
    public function emailRemove(){
        global $islogin_admin;
       if($islogin_admin!=1)exitjson(-1,"请先登录！");
        $id = input("post")['id'];
        $sql = "SELECT * FROM `pay_email` WHERE `id`='{$id}' limit 1"; 
        if(!executeSQLQuery($sql))exitjson(-1,'此数据不存在！');
        $sql ="DELETE FROM pay_email WHERE `id`='{$id}' limit 1";
        if(executeSQLQuery($sql, 'delete')){
           exitjson(200,"删除发信记录成功！");
       }
       exitjson(-1,'删除失败！');
    }
}

?>